package com.naizhuai.ui.task;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.naizhuai.AppContext;
import com.naizhuai.network.Config;
import com.naizhuai.network.RetrofitServerFactory;
import com.naizhuai.network.apistore.TaskActionApiRequest;
import com.naizhuai.network.apistore.TaskActionApiStore;
import com.naizhuai.ui.entity.CommonLabel;
import com.naizhuai.ui.entity.AddQuestion;
import com.naizhuai.ui.entity.AddTask;
import com.naizhuai.ui.entity.AddTaskPage;
import com.naizhuai.ui.entity.NormalQAPage;
import com.naizhuai.ui.entity.StepJoin;
import com.naizhuai.ui.entity.StepSettingBean;
import com.naizhuai.ui.entity.Task.AddTaskFlowPage;
import com.naizhuai.ui.entity.Task.FlowMethodDetial;
import com.naizhuai.ui.entity.Task.BehaviorRecommendation;
import com.naizhuai.ui.entity.Task.TaskChartData;
import com.naizhuai.ui.entity.TaskDetialEntity;
import com.naizhuai.ui.entity.TaskFlowMethod;
import com.naizhuai.ui.entity.TaskListBean;
import com.naizhuai.ui.entity.TempImgBean;
import com.naizhuai.ui.entity.UserLeader;
import com.naizhuai.util.RxUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Observable;

/**
 * @author：lichenxi
 * @date 2018/3/29 15
 * email：525603977@qq.com
 * Fighting
 */
public class TaskActionApiIml implements TaskActionApiStore {

    private static TaskActionApiIml INSTANCE = null;

    public static TaskActionApiIml getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new TaskActionApiIml();
        }
        return INSTANCE;
    }

    @Override
    public Observable<TaskFlowMethod> getTaskFlowList() {
        return createBaseClass().getTaskFlowList()
                                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> addTaskPage(AddTask addTask) {
        Map<String,String> hashMap=new HashMap();
        hashMap.put("goalId",addTask.getGoalId());
        hashMap.put("name",addTask.getName());
        hashMap.put("detail",addTask.detail);
        hashMap.put("leaderUid",addTask.leaderUid);
        hashMap.put("budget",addTask.budget);
        hashMap.put("keyResult",addTask.keyResult);
        hashMap.put("currentValue",addTask.currentValue);
        hashMap.put("goalValue",addTask.goalValue);
        hashMap.put("weekControl",addTask.weekControl);
        return createBaseClass()
                .AddTask(hashMap)
                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<UserLeader>> User_Leader(String id) {
        return createBaseClass().USER_LEADER(id).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<StepSettingBean> leader_set_task_flow_page(String taskFlowId) {
        return createBaseClass().leader_set_task_flow_page(taskFlowId).compose(RxUtils.applySchedulers());
    }

    private TaskActionApiRequest createBaseClass() {
        return RetrofitServerFactory.create(AppContext.getInstance(), TaskActionApiRequest.class, Config.TEST_ACTION_BASE_URL);
    }

    @Override
    public Observable<TaskListBean> getTask_List(String companyId) {
        return createBaseClass().TASK_LIST(companyId).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> leader_set_task_flow(String taskFlowId, String imageUrl, String detail, String stepName, String circleType) {
        return createBaseClass().leader_set_task_flow(taskFlowId,imageUrl,detail,stepName,circleType)
                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<TaskDetialEntity> task_detail(String taskId) {
        return createBaseClass().task_detail(taskId).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<AddTaskFlowPage> add_task_flow_page(String taskId, String companyId) {
        return createBaseClass().add_task_flow_page(taskId,companyId).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> add_task_flow(String taskId, String flowData) {
        return createBaseClass().add_task_flow(taskId,flowData).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<FlowMethodDetial>> task_flow_method_detail(String methodId) {
        return createBaseClass().task_flow_method_detail(methodId).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<StepJoin> task_flow_joined(String taskFlowId) {
        return createBaseClass().task_flow_joined(taskFlowId).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<AddTaskPage> getAddTaskPage(String companyId, String goalid) {
        return createBaseClass().getAddTaskPage(companyId,goalid).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<NormalQAPage> normal_question_page(String questionId, String schemeId) {
        return createBaseClass().normal_question_page(questionId,schemeId)
                                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> receive_question(String questionId, String descLabel, String schemeId) {
        return createBaseClass().receive_question(questionId,descLabel,schemeId)
                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> add_normal_question(AddQuestion addQuestion) {
        Map<String,String> hashMap=new HashMap();
        if (!TextUtils.isEmpty(addQuestion.getSchemeId())){
            hashMap.put("schemeId",addQuestion.getSchemeId());
        }
        if (!TextUtils.isEmpty(addQuestion.getTaskFlowId())){
            hashMap.put("taskFlowId",addQuestion.getTaskFlowId());
        }
       if (null!=addQuestion.getCycleTypes() && addQuestion.getCycleTypes().size()>0){
           hashMap.put("weekControl",new Gson().toJson(addQuestion.getCycleTypes()));
       }
        hashMap.put("departmentId",addQuestion.getDepartmentId());
        hashMap.put("descLabel",addQuestion.getDescLabel());
        hashMap.put("title",addQuestion.getTitle());
        hashMap.put("imgs",addQuestion.getImgs());
        hashMap.put("managerUid",addQuestion.getManagerUid());
        return createBaseClass().add_normal_question(hashMap).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<CommonLabel>> sub_label_list(String labelId) {
        return createBaseClass().sub_label_list(labelId).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<TaskChartData>> task_data_page(String taskid) {
        return createBaseClass().task_chart_data_page(taskid).compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<List<BehaviorRecommendation>> label_question_search(String title, String labels) {
        return createBaseClass().label_question_search(title,labels)
                                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<String> add_task_data_page(String taskid, String chartData) {
        return createBaseClass().add_task_data_page(taskid,chartData)
                .compose(RxUtils.applySchedulers());
    }

    @Override
    public Observable<TempImgBean> get_scheme_image(String schemeId) {
        return createBaseClass().get_scheme_image(schemeId).compose(RxUtils.applySchedulers());
    }
}
